﻿Option Strict On
Imports System.Threading
Imports System.Globalization

''' <summary>
''' Displays and handles errors and exceptions related to file I/O operations.  Includes functionality to test for existence of files and directories. 
''' </summary>
Module FileErrorHandler

    Private oCurrentCulture As Globalization.CultureInfo = Thread.CurrentThread.CurrentCulture     'used for string propercase conversion
    Private oTextInfo As TextInfo = oCurrentCulture.TextInfo()

    ''' <summary>
    ''' Indicates the existence of a specified directory.
    ''' </summary>
    ''' <param name="path">Full path of directory to check.</param>
    ''' <returns>True if directory exists, otherwise false.</returns>
    Public Function DirectoryExists(ByVal path As String) As Boolean

        If IO.Directory.Exists(path) Then Return True
        DisplayErrorDialog(String.Format(My.Resources.DialogTitleNotFoundError, oTextInfo.ToTitleCase(My.Resources.GenericStringDirectory)), String.Format(My.Resources.ErrorCannotLocate, My.Resources.GenericStringDirectory))
        Return False

    End Function

    ''' <summary>
    ''' Displays an error dialog with the specified text, title and optional exception details.
    ''' </summary>
    ''' <param name="title">Title to appear in dialog box</param>
    ''' <param name="text">Text to be displayed in dialog box.</param>
    ''' <param name="ex">Optional exception to be displayed in dialog box (verbose).</param>
    Private Sub DisplayErrorDialog(ByVal title As String,
                                   ByVal text As String,
                                   Optional ByVal ex As Exception = Nothing)

        If ex IsNot Nothing Then MessageBox.Show(text & vbCrLf & vbCrLf & ex.ToString, title, MessageBoxButtons.OK, MessageBoxIcon.Error) Else MessageBox.Show(text, title, MessageBoxButtons.OK, MessageBoxIcon.Error)

    End Sub

    ''' <summary>
    ''' Indicates the existence of a specified file.
    ''' </summary>
    ''' <param name="path">Full path of file to check.</param>
    ''' <returns>True if file exists, otherwise false.</returns>
    Public Function FileExists(ByVal path As String) As Boolean

        If IO.File.Exists(path) Then Return True
        DisplayErrorDialog(String.Format(My.Resources.DialogTitleNotFoundError, oTextInfo.ToTitleCase(My.Resources.GenericStringFile)), String.Format(My.Resources.ErrorCannotLocate, My.Resources.GenericStringFile))
        Return False

    End Function

    ''' <summary>
    ''' Handles and displays error for specified exception.
    ''' </summary>
    ''' <param name="ex">Exception to be handled.</param>
    ''' <param name="errorSourceName">Optional name indicating source of error.</param>
    ''' <returns>True if exception handled and error displayed; otherwise false.</returns>
    Public Function HandleFileError(ByVal ex As Exception,
                                    Optional ByVal errorSourceName As String = vbNullString) As Boolean

        If TypeOf ex Is IO.DirectoryNotFoundException OrElse
            TypeOf ex Is IO.DriveNotFoundException Then
            DisplayErrorDialog(String.Format(My.Resources.DialogTitleNotFoundError, oTextInfo.ToTitleCase(My.Resources.GenericStringDirectory)), String.Format(My.Resources.ErrorInvalidFolder, errorSourceName, ex.Message))
            Return True
        End If

        If TypeOf ex Is IO.IOException Then
            DisplayErrorDialog(My.Resources.DialogTitleFileIOError, String.Format(My.Resources.ErrorInvalidFolder, errorSourceName, ex.Message))
            Return True
        End If

        If TypeOf ex Is System.UnauthorizedAccessException Then
            DisplayErrorDialog(My.Resources.DialogTitleUnauthError, String.Format(My.Resources.ErrorInvalidFolder, errorSourceName, ex.Message))
            Return True
        End If

        DisplayErrorDialog(My.Resources.DialogTitleUnhandledException, My.Resources.ErrorUnspecified, ex)  'display verbose error
        Return False    'error wasn't handled successfully

    End Function

End Module
